package iotwisp.school.api.controller.pc_admin.system;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.ValidationUtils;
import iotwisp.school.system.entity.City;
import iotwisp.school.system.qo.CityQO;
import iotwisp.school.system.service.CityService;

@RestController
@RequestMapping("/api/system/city")
public class CityController extends RestBaseController {

	@Autowired
	private CityService cityService;

	@PostMapping("/query")
	public ResponseEntity<JSONResultDTO> queryCity(@RequestBody CityQO qo)
			throws UnsupportedEncodingException, BusinessException {

		ValidationUtils.validate(qo);
		
		switch (qo.getResultType()) {
		case CityQO.RESULT_TYPE_COUNT:
			Integer count = cityService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case CityQO.RESULT_TYPE_UNIQUE:
			City city = cityService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(city));
		case CityQO.RESULT_TYPE_LIST:
			List<City> cities = cityService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(cities));
		case CityQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = cityService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}

}
